package com.demo.gulimall.search.controller;

import com.demo.gulimall.common.exception.BizCodeEnume;
import com.demo.gulimall.common.to.es.ProductEsTo;
import com.demo.gulimall.common.utils.R;
import com.demo.gulimall.search.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author Chen Yuan
 * @create 2023-05-29  14:49
 */
@Slf4j
@RestController
@RequestMapping("/es/search")
public class ElasticSearchController {

	@Autowired
	private ProductService productService;

	// 上架
	@PostMapping("/save/product")
	public R productStatusUp(@RequestBody List<ProductEsTo> productEsTos){
		Boolean hasFailures;
		try {
			hasFailures = productService.productStatusUp(productEsTos);
		} catch (IOException e) {
			log.error("-- ElasticSearchController - 商品上架异常 \n", e);
			return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
		}

		if (hasFailures) {
			return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
		}

		return R.ok();
	}
}
